#
#   pimento/option.rb - Pimento WebDAV client
#   	$Release Version: 0.0.1$
#   	$Revision: $
#   	$Date: $
#   	by Curl-Ojisan
#

require 'pp'
require 'kconv'
require 'net/https'

module Pimento

  # オプション定義
  module Option

    # パーサを生成
    require 'optparse'
    @@parser = OptionParser.new

    # ヘルプメッセージの先頭バナーを設定
    @@parser.banner = <<-EOT

Usage:
  #{PIMENTO} [options] <command> [arguments]

    EOT

    class << @@parser

      def full_help
        # 追加のヘルプメッセージを連結
        help + <<-EOT

Examples:
  #{PIMENTO} put foo.txt http://dav.example.com/
  #{PIMENTO} help put

Further help:
  #{PIMENTO} help commands	list all '#{PIMENTO}' commands
  #{PIMENTO} help <COMMAND>	show help on COMMAND

Further information:
  http://pimento.googlecode.com

        EOT
      end


    end

    # オプションモジュールの格納先
    @@option_modules = []

    # 定義されたオプションモジュールを格納していく
    def self.included(mod)
      @@option_modules << mod
    end

    # オプションインスタンスの格納先
    @@option_instances = {}

    # オプションインスタンスへのアクセサ
    def self.[](name)
      @@option_instances[name.to_sym]
    end

    # オプションを解読する
    def self.parse!(argv)

      # -v/--verbose オプションだけ先に読んでおく
      argv.each {|v|
        Verbose.new(true) if v == '-v' or v == '--verbose'
      }

      # 全オプションモジュールをOptionParserに登録する
      @@option_modules.each {|om|
        @@parser.on(*om.option) {|v|
          @@option_instances[om.name] = om.new(v)
        }
      }

      # オプション解読を実施する
      Pimento.vputs '----'
      Pimento.vputs 'Parsing options ...'
      @@parser.parse!(argv)
      Pimento.vputs 'Parsed options ...'
      Pimento.vputs '----'

    end

    # cURL用オプションを返す
    def self.curl_options

      # 全オプションインスタンスからcURL用オプションを集める
      @@option_instances.values.map {|oi| oi.curl_option }.join(' ')

    end

    attr_accessor :value

    class Auth
      include Option
      def self.option
        ["--auth=<'basic'>", [:basic], 'Set authentication method']
      end
      def initialize(v)
        @value = v
        Pimento.vputs "auth:		#{value}"
      end
      def curl_option
        Pimento.vp value
        case value
        when :basic
          return '--basic'
        else
          return ''
        end
      end
    end

    class User
      include Option
      def self.option
        ['-u', '--user=<user[:password]>', 'Set server user and password']
      end
      def initialize(v)
        @value = v
        Pimento.vputs "user:		#{user}"
        Pimento.vputs "password:	#{password}"
      end
      def curl_option
        "--user \"#{user}:#{password}\""
      end
      def user
        value.split(/:/)[0]
      end
      def password
        value.split(/:/)[1]
      end
      def basic_credencial
        'Basic ' + Base64.encode64(user + ':' + password)
      end
    end

    class CACert
      include Option
      def self.option
        ['--cacert=<file>', 'CA certificate to verify peer against (SSL)']
      end
      def initialize(v)
        @value = v
        Pimento.vputs "cacert:		#{value}"
      end
      def curl_option
        "--cacert  \"#{value}\""
      end
    end

    class Help
      include Option
      def self.option
        ['-h', '--help', 'Print this messages']
      end
      def initialize(v)
        puts @@parser.full_help
        exit
      end
      def curl_option
        ''
      end
    end

    class Version
      include Option
      def self.option
        ['-V', '--version', "Print the version of #{PIMENTO}"]
      end
      def initialize(v)
        puts @@parser.version
        exit
      end
      def curl_option
        ''
      end
    end

    class Verbose
      include Option
      def self.option
        ['-v', '--verbose', 'Make the operation more talkative']
      end
      def initialize(v)
        Pimento.verbose = v
        Pimento.vputs "verbose:		#{value}"
      end
      def curl_option
        ''
      end
    end

  end

end

# vi: ts=8 sw=2 tw=72 fenc=utf-8
# END OF FILE
